#include "FrntTurnLampSnapshot.h"

void updateFrntTurnLampSnapshot(bool isPosnOn, bool isDayTimeOn, const FrntTurnLampSnapshot* const lastSnapshot,FrntTurnLampSnapshot* output)
{
    if(isPosnOn && isDayTimeOn)
    {
        uint8_t lastLampState = lastSnapshot->records_ & 0x0f;
        if(lastLampState == 0x03) //last both dayTime and posn
        {
            // Ignore
        }
        else if(lastLampState == 0x01) // last is posn 0000 0001
        {
            output->records_ = 0x23;// 0010 0011  remember last turn on lamp is day time lamp, two lamp both turn on now
        }
        else if(lastLampState == 0x02)// last is daytime 0000 0010
        {
            output->records_ = 0x13;// 0001 0011  remember last turn on lamp is posn lamp, two lamp both turn on now
        }
        else
        {
            // error
        }
    }
    else if(isDayTimeOn)
    {
        output->records_ = 0x22;// 0010 0010  remember last turn on lamp is day time lamp, and only daytime lamp turn on now
    }
    else if(isPosnOn)
    {
        output->records_ = 0x11;// 0001 0001  remember last turn on lamp is posn lamp, and only daytime lamp turn on now
    }
    else
    {
        output->records_ = 0x00;
    }
}

NormalState getNormalStateWithFrntTurnLampSnapshot(const FrntTurnLampSnapshot* const snapshot)
{
    uint8_t lastTurnOnLamp = snapshot->records_ & 0xf0;     // get last turn on lamp
    uint8_t lastWorkedLamp = snapshot->records_ & 0x0f; // get last should turn on lamp
    NormalState output = NormalStateLeFrntTurnLamp_Invaild;
    if(0x03 == lastWorkedLamp)  //two lamp both turn on
    {
        if(lastTurnOnLamp == 0x10)  // last turn on lamp is posm
        {
            output = NormalStateLeFrntTurnLamp_DayTimeAndPosBoth_ButPosn;
        }
        else if(lastTurnOnLamp == 0x20)
        {
            output = NormalStateLeFrntTurnLamp_DayTimeAndPosBoth_ButDayTime;
        }
        else
        {
            //I really dont know!!!
            output = NormalStateLeFrntTurnLamp_DayTimeAndPosBoth_ButDayTime;
        }
    }
    else if(0x02 == lastWorkedLamp)
    {
        output = NormalStateLeFrntTurnLamp_DayTime;
    }
    else if(0x01 == lastWorkedLamp)
    {
        output = NormalStateLeFrntTurnLamp_Posn;
    }
    else
    {
        //I really dont know!!!
        output = NormalStateLeFrntTurnLamp_Posn;
    }

    return output;
}
